package com.dubito.formvalidation.screen

import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.dubito.formvalidation.domain.UIEvent
import com.dubito.formvalidation.domain.ValidationEvent

/**
 * author: LiLiang
 * createTime: 2022/9/1-13:45
 * remark:
 **/

@Composable
fun BankForm(bankFormViewModel: BankFormViewModel = viewModel()) {
    val state = bankFormViewModel.uiState.value
    val context = LocalContext.current
    val localFocus = LocalFocusManager.current
    LaunchedEffect(key1 = context) {
        bankFormViewModel.validationEvent.collect { event ->
            when (event) {
                ValidationEvent.Success -> {
                    Toast.makeText(context, "输入全部验证成功!", Toast.LENGTH_LONG).show()
                }
            }
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(8.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        BankInputField(
            label = "账号",
            onTextChanged = { bankFormViewModel.onEvent(UIEvent.AccountChanged(it)) },
            isError = state.hasAccountError,
            keyboardType = KeyboardType.Number,
            onNext = { localFocus.moveFocus(FocusDirection.Down) },
            onDone = {}
        )
        BankInputField(
            label = "确认账号",
            onTextChanged = { bankFormViewModel.onEvent(UIEvent.ConfirmAccountChanged(it)) },
            keyboardType = KeyboardType.NumberPassword,
            isError = state.hasConfirmAccountError,
            onNext = {
                localFocus.moveFocus(FocusDirection.Down)
            },
            onDone = {}
        )
        BankInputField(
            label = "银行编码",
            onTextChanged = { bankFormViewModel.onEvent(UIEvent.CodeChanged(it)) },
            isError = state.hasCodeError,
            onNext = { localFocus.moveFocus(FocusDirection.Down) },
            onDone = {}
        )
        BankInputField(
            label = "所有人",
            onTextChanged = { bankFormViewModel.onEvent(UIEvent.NameChanged(it)) },
            isError = state.hasNameError,
            imeAction = ImeAction.Done,
            onNext = {},
            onDone = {
                localFocus.clearFocus()
            }
        )
        Spacer(modifier = Modifier.weight(1f))
        Button(onClick = { bankFormViewModel.onEvent(UIEvent.Submit) }) {
            Text(text = "提交")
        }
    }
}